// userPageA/pages/billList/billList.js
const app = getApp()
const WXAPI = require('../../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '账单', // 导航栏 中间的标题
      textColor: '#000', // 标题颜色
      bgColor: '#fff', // 导航栏背景颜色
      isloading: false,
      isColor: true,
    },
    retrieveType:[
      {id: '0',value:'资产类型'},
      {id: '1',value:'出入类型'},
      // {id: '2',value:'订单类型'},
    ],
    outType:[
      {id:'',value: '不限'},
      {id:'1',value: '转出'},
      {id:'2',value: '转入'},
    ],
    asset_type: [{id:'',value: '不限'}],//资产类型
    customer_asset_change_log_use_type: [{id:'',value: '不限'}],//收支类型
    customer_asset_change_log_business_type: [{id:'',value: '不限'}],//收支子类型
    dlone_list:[],
    currentNavtab: '',
    isdlong:false,
    assetType: '',
    type: '',
    pageNum: 1,
    pageSize: 10,
    isPull: true,
    lodingtype: '',
    billList:[],//账单列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDict()
    this.getList()
  },
  //获取资产类型
  getDict(){
    let that = this   
    var dictType = ['asset_type','customer_asset_change_log_use_type','customer_asset_change_log_business_type']//
    var data = {
      dictType: dictType.join()
    }
    WXAPI.getUserDict( data, (res) =>{
      const data = res.result
      if(res.code === 200){
        console.log("获取资产类型",data)
        that.data.asset_type = that.data.asset_type.concat(data[0].dictList)
        that.data.customer_asset_change_log_use_type = that.data.customer_asset_change_log_use_type.concat(data[1].dictList)
        that.data.customer_asset_change_log_business_type = that.data.customer_asset_change_log_business_type.concat(data[2].dictList)
        that.setData({
          asset_type: that.data.asset_type,
          customer_asset_change_log_use_type: that.data.customer_asset_change_log_use_type,
          customer_asset_change_log_business_type: that.data.customer_asset_change_log_business_type,
        })
      }
    })
  },
  //关闭弹窗
  handleChose(){
    this.setData({
      isdlong: false,
    })
  },
  handleType(e){
    var id = e.currentTarget.dataset.id;
    var isdlong = ''
    console.log('id',id)
    if(this.data.isdlong){
      if(this.data.currentNavtab == id){
        isdlong = false
      }else{
        isdlong = true
      }
    }else{
      isdlong = true
    }
    
    this.setData({
      currentNavtab: id,
      isdlong: isdlong,
    })
    console.log('this.data.currentNavtab',this.data.currentNavtab)
    console.log('this.data.isdlong',this.data.isdlong)
    this.getdlone_list()
  },
  getdlone_list(){
    var currentNavtab = this.data.currentNavtab
    if(currentNavtab == 0){
      this.data.dlone_list = this.data.asset_type
    }else if(currentNavtab == 1){
      this.data.dlone_list = this.data.outType
    }
    this.setData({
      dlone_list: this.data.dlone_list
    })
    console.log('this.data.dlone_list',this.data.dlone_list)
  },
  //弹出框选择
  handleDlong(e){
    var id = e.currentTarget.dataset.id
    var currentNavtab = this.data.currentNavtab
    if(currentNavtab == 0){
      this.setData({
        assetType: id
      })
    }else if(currentNavtab == 1){
      this.setData({
        type: id
      })
    }
    this.setData({
      pageNum: 1,
      isPull: true,
      isdlong: false,
      billList: [],
    })
    this.getList()
  },
  //列表
  getList(){
    var data = {
      assetType: this.data.assetType,
      type: this.data.type,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
    }
    WXAPI.getchangelog(data,(res)=>{
      console.log('账单列表',res)
      const {size,total,records} = res.result
      var billList = this.data.billList
      if(billList.length == 0){
        billList = billList.concat(records)
      }else{
        billList.map(list =>{
          records.map(item=>{
            if(item.groupTime == list.groupTime){
              list.children = list.children.concat(item.children)
              return list.children
            }else{
              billList = billList.concat(item)
              return billList
            }
          })
          // console.log('item-1',item)
        })
      }
      let hash = {};
      billList = billList.reduce((preVal, curVal) => {
        console.log(' hash[curVal.groupTime]', hash[curVal.groupTime])
        hash[curVal.groupTime] ? '' : hash[curVal.groupTime] = true && preVal.push(curVal);
        return preVal
      }, [])
      console.log('账单列表',billList)
      if(this.data.pageNum >= Math.ceil(total / size)){
        console.log('没有分页+++++++++++')
          this.setData({
            lodingtype: 1,
            isPull: false
          })
      }else{
        this.setData({
          lodingtype: 0
        })
      }
      this.setData({
        billList: billList
      })
    })
  },
  //跳转详情
  handleDetail(e){
    var id = e.currentTarget.dataset.id
    var businessType = e.currentTarget.dataset.business
    console.log('e.currentTarget.dataset.',businessType)
    if(businessType == 3){//转赠详情
      wx.navigateTo({
        url: '/userPageA/pages/examplesDetail/examplesDetail?id='+id,
      })
    }else{
      wx.navigateTo({
        url: '/userPageA/pages/billDetail/billDetail?id='+id,
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  onReachBottom: function() {
    if(this.data.isPull==true){
        var pageNum = this.data.pageNum
        pageNum ++
        console.log("触底了",pageNum)
        this.setData({
          pageNum: pageNum
        });
        this.getList()
      
    }
    // this.getticketList()
  },


})